package com.honglang.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.honglang.common.tools.Constants;
import com.honglang.common.tools.Pager;
import com.honglang.dao.FileSysDao;
import com.honglang.entity.FileSys;


public class FileSysServiceImpl  implements FileSysService{
	
	private FileSysDao fileSysDao;
	
	/**
	 * save fileSys entities to database;
	 * @param realContextPath
	 * @param fileNameList
	 */
	public void addFileSys(String realContextPath,List<String> fileNameList){
		List<FileSys> fileSysList = new ArrayList<FileSys>();
		
		if(null != fileNameList){
			for(String fileName : fileNameList){
				
				String relativeUrl ="";
				if(Constants.ImageTypesList.contains(fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase())){
					relativeUrl = Constants.UploadFileRootDir + "/" +Constants.ImageFileDir + "/" + fileName;
				}else{
					relativeUrl = Constants.UploadFileRootDir + "/" +Constants.OtherFileDir + "/" + fileName;
				}
				FileSys fileSys = new FileSys();
				fileSys.setRelativeUrl(relativeUrl);
				fileSys.setAbsoluteUrl(realContextPath + relativeUrl  );
				fileSys.setFileName(fileName);
				fileSys.setSuffix(fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase());
				fileSys.setUploadDate(new Date());
				fileSysList.add(fileSys);
				
			}
			this.getFileSysDao().batchSaveOrUpdate(fileSysList);
		}
	}
	
	
	/**
	 * get filesys by relativeUrl;
	 * @param relativeUrl
	 * @return
	 */
	public FileSys getFileSysByRelativeUrl(String relativeUrl){
		DetachedCriteria dc = DetachedCriteria.forClass(FileSys.class);
		dc.add(Restrictions.eq("relativeUrl", relativeUrl));
		return this.getFileSysDao().getByDetachedCriteria(dc).get(0);
		
	}
	
	/**
	 * delete filesys;
	 * @param fileSys
	 */
	public void  deleteByRelativeUrl(FileSys fileSys){
		
		this.getFileSysDao().delete(fileSys);
	}
	
	
	/**
	 * get non image filesys in page
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public Pager getNonImageFileInPager(int pageSize,int pageNo){
		DetachedCriteria dc = DetachedCriteria.forClass(FileSys.class);
		dc.add(Restrictions.not(Restrictions.in("suffix", Constants.ImageTypesList)));
		dc.addOrder(Order.asc("uploadDate"));
		DetachedCriteria dcCount = DetachedCriteria.forClass(FileSys.class);
		dcCount.add(Restrictions.not(Restrictions.in("suffix", Constants.ImageTypesList)));
		dcCount.addOrder(Order.asc("uploadDate"));
		List<FileSys> fileSysList = this.getFileSysDao().getByDetachedCriteriaInPager(dc,  pageSize*(pageNo - 1), pageSize);
		Pager pager = new Pager();
		pager.CreatePager(pageSize, pageNo, Constants.pageIndexCountPerPage, this.getFileSysDao().getRowCountByDetachedCriteria(dcCount),fileSysList);
		return pager;
	}
	
	
	/**
	 * get by ID
	 * @param fileSysId
	 * @return
	 */
	public FileSys getFileSys(Integer fileSysId){
		return this.getFileSysDao().getById(fileSysId);
	}
	
	
	
	/**
	 * delete fileSys entity
	 * @param fileSys
	 */
	public void delete(FileSys fileSys){
		this.getFileSysDao().delete(fileSys);
	}
	
	
	
	/**
	 * delete fileSys by Id
	 * @param fileSysId
	 */
	public void delete(Integer fileSysId){
		 this.getFileSysDao().delete(fileSysId);
	}
	
	
	/**
	 * get fileSys by fileSys Ids; like "1-10-22"
	 * @param fileSysIds
	 * @return
	 */
	public List<FileSys> getFileSysByIds(String fileSysIds){
		List<Integer> idList = new ArrayList<Integer>();
		for(String id : fileSysIds.split(Constants.FILE_SYS_SPILIT)){
			idList.add(Integer.valueOf(id));
		}
		DetachedCriteria dc = DetachedCriteria.forClass(FileSys.class);
		dc.add(Restrictions.in("id", idList));
		return this.getFileSysDao().getByDetachedCriteria(dc);
	}
	
	
	/**
	 * batch delete fileSys
	 * @param fileSysList
	 */
	public void delete(List<FileSys> fileSysList){
		this.getFileSysDao().deleteAll(fileSysList);
	}
	
	
	
	public FileSysDao getFileSysDao() {
		return fileSysDao;
	}

	public void setFileSysDao(FileSysDao fileSysDao) {
		this.fileSysDao = fileSysDao;
	}
	
}
